package com.bua.toothfactory;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.*;
import com.bua.control.MultipleCheckButton;
import com.bua.control.SingleCheckButton;
import com.bua.control.ZZTPaint;
import com.bua.datafactory.DataFactory;
import com.bua.datafactory.DesignDetailDataFactory;
import com.bua.net.NetConfig;
import com.bua.net.NetConfig.HttpRequestId;
import com.bua.net.RequestTask;
import com.bua.net.ResponseReport;
import com.bua.net.Tool;
import com.bua.net.obj.CreateZhongzhitiOrderObject;
import com.bua.toothfactory.config.Config;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import org.json.JSONException;

public class ZhongzhitiOrderActivity extends Activity implements ResponseReport{

    private SingleCheckButton scbJGS=null;
    private TextView tvDesignId=null;
    private TextView tvCreateDate=null;


	private EditText etZZTCount;
	private SingleCheckButton scbGDFS;
	private CheckBox cbHMKK;
	private CheckBox cbYYCXS;
	private MultipleCheckButton mcbZZFZW;
	private MultipleCheckButton mcbFDDZZTPJ;
	private EditText etFDDZZPJCount;
	private MultipleCheckButton mcbCMXWDFZW;
	private LinearLayout zztPaint;
	private Button btnCreateDesignOrder;

    private String orderId = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zhongzhiti_order_activity);

        Intent intent = this.getIntent();
        //designId = intent.getStringExtra("designId");
        orderId = intent.getStringExtra("orderId");

        tvDesignId = (TextView) findViewById(R.id.tvDesignId);
        scbJGS = (SingleCheckButton) findViewById(R.id.scbJGSSelector);
        scbJGS.items=Config.JGSList;
        tvCreateDate = (TextView) findViewById(R.id.tvCreateDate);


		etZZTCount = (EditText) findViewById(R.id.etZZTCount);
		scbGDFS = (SingleCheckButton) findViewById(R.id.scbGDFS);
		scbGDFS.SetItems(Config.shangbuhuodongxiufu);
		cbHMKK = (CheckBox) findViewById(R.id.icbHMKK);
		cbYYCXS = (CheckBox) findViewById(R.id.icbYYCXS);
		mcbZZFZW = (MultipleCheckButton) findViewById(R.id.mcbZZFZW);
		mcbZZFZW.SetItems(Config.zhongzhifuzhuwu);

		mcbFDDZZTPJ = (MultipleCheckButton) findViewById(R.id.mcbFDDZZPJ);
		mcbFDDZZTPJ.SetItems(Config.zhongzhipeijian);
		etFDDZZPJCount = (EditText) findViewById(R.id.etFDDZZPJCount);
		mcbCMXWDFZW = (MultipleCheckButton) findViewById(R.id.mcbCMXWDWZW);
		mcbCMXWDFZW.SetItems(Config.moxingwaifushuwu);
		
		zztPaint=(LinearLayout)findViewById(R.id.zztPaint);
		ZZTPaint paint=new ZZTPaint(this);
		zztPaint.addView(paint);
		btnCreateDesignOrder=(Button)findViewById(R.id.btnCreateDesignOrder);
		btnCreateDesignOrder.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
                ZhongzhitiOrderActivity.this.CreateDesignOrder();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.zhongzhiti_order, menu);
		return true;
	}

    private void CreateDesignOrder() {
        CreateZhongzhitiOrderObject OrderObject=new CreateZhongzhitiOrderObject();
        OrderObject.zztCount=etZZTCount.getText().toString();
        OrderObject.gudingfangshi=scbGDFS.GetTitle();
        OrderObject.heiankaikong=cbHMKK.isChecked();
        OrderObject.yayincixiushi=cbYYCXS.isChecked();
        OrderObject.zhongzhifuzhuwu=mcbZZFZW.GetTitle();
        OrderObject.fudaidezhongzhipeijian=mcbFDDZZTPJ.GetTitle();
        OrderObject.peijianCount=etFDDZZPJCount.getText().toString();
        OrderObject.chumoxingwaidefuzhuowu=mcbCMXWDFZW.GetTitle();
        String Image=null;
        zztPaint.setBackgroundColor(Color.WHITE);
        zztPaint.setDrawingCacheEnabled(true);
        Bitmap bmPicture=zztPaint.getDrawingCache();
        Bitmap mBitmap = Bitmap.createBitmap(bmPicture, 0, 0, bmPicture.getWidth(), 484);
        ByteArrayOutputStream pictureOutput = new ByteArrayOutputStream();
        if (mBitmap!=null &&  mBitmap.compress(CompressFormat.JPEG, 50, pictureOutput))
            Image = Base64.encodeToString(pictureOutput.toByteArray(),
                    Base64.DEFAULT);
        String JGSId=  scbJGS.GetTitle().split(" ")[0];
        if(JGSId==null || JGSId.length()<=0)
        {
            Tool.showToast(this, "请输入完整信息");
            return;
        }
         String url= NetConfig.creatUrl(HttpRequestId.CREATE_DESIGN_ORDER);
        RequestTask request = new RequestTask();
        HashMap<String, String> requestData =NetConfig.CreateDesignOrder(orderId,"ZZT",JGSId,Image,OrderObject.CreateOrderObject());
        request.CreateTask(url, requestData, NetConfig.HttpRequestId.CREATE_DESIGN_ORDER.ordinal(), this);
    }
    @Override
    public void callBack(String result, int requestId) {
        //To change body of implemented methods use File | Settings | File Templates.
        NetConfig.HttpRequestId RequestId = NetConfig.HttpRequestId.values()[requestId];
        switch (RequestId) {
            case CREATE_DESIGN_ORDER:
            {
                DataFactory resultData=new DataFactory(result);
                if(resultData.status==200)
                {
                    try {
                        tvDesignId.setText("设计定单编号：" +resultData.body.getString("designID"));
//                        tvCreateDate.setText(resultData.body.getString("created"));
                        this.finish();
                    }   catch (JSONException e)
                    {

                    }

                }
            }
            break;
            case GET_DESIGN_ORDER_DETAIL:
            {
                SetDesignOrder (result);
            }
            break;
            default:
                break;
        }
    }

    private void SetDesignOrder(String jsonString) {
        DesignDetailDataFactory obj = new DesignDetailDataFactory(jsonString);
        tvCreateDate.setText("创建日期：" + obj.createdDate);
        tvDesignId.setText("设计单编号：" + obj.designId);
        scbJGS.setText(obj.jigongsuo);
        btnCreateDesignOrder.setVisibility(View.INVISIBLE);

        etZZTCount.setText(obj.GetItem("种植体数量").toString());
        etZZTCount.setEnabled(false);
        scbGDFS.setText(obj.GetItem("前牙固定方式").toString());
        scbGDFS.setEnabled(false);
        cbHMKK.setChecked((Boolean)obj.GetItem("颌面开孔"));
        cbHMKK.setEnabled(false);
        cbYYCXS.setChecked((Boolean)obj.GetItem("牙龈瓷修饰"));
        cbYYCXS.setEnabled(false);
        mcbZZFZW.setText(obj.GetItem("种植辅助物").toString());
        mcbZZFZW.setEnabled(false);

        mcbFDDZZTPJ.setText(obj.GetItem("附带的种植配件").toString());
        mcbFDDZZTPJ.setEnabled(false);

        etFDDZZPJCount.setText(obj.GetItem("配件个数").toString());
        etFDDZZPJCount.setEnabled(false);
        mcbCMXWDFZW.setText(obj.GetItem("除摸型外的辅助物").toString());
        mcbCMXWDFZW.setEnabled(false);

        zztPaint=(LinearLayout)findViewById(R.id.zztPaint);
        ImageView image = new ImageView(this);
        image.setImageBitmap(BitmapFactory.decodeByteArray(obj.designImage, 0, obj.designImage.length));
        zztPaint.addView(image);
    }
}
